using System.Collections.Generic;
using System.Text;
using ComponentSystem;
using UnityEngine;
using ZYFramework.ObjectPool;

namespace ZYFramework.AudioSystem
{
    /// <summary>
    /// 音频调试监控组件
    /// </summary>
    public class Com_AudioDebugMonitor : ComponentWithId
    {
        /// <summary>
        /// 是否启用调试模式
        /// </summary>
        private bool _enableDebug = false;

        /// <summary>
        /// 资源管理组件
        /// </summary>
        private Com_AudioResourceManager _resourceManager;

        /// <summary>
        /// 对象池组件
        /// </summary>
        private Com_AudioSourcePool _sourcePool;

        /// <summary>
        /// 播放控制组件
        /// </summary>
        private Com_AudioPlayController _playController;

        /// <summary>
        /// 全局控制组件
        /// </summary>
        private Com_AudioGlobalControl _globalControl;

        protected override void Awake()
        {
            base.Awake();
        }

        protected override void Start()
        {
            base.Start();
        }

        /// <summary>
        /// 初始化调试监控
        /// </summary>
        /// <param name="resourceManager">资源管理组件</param>
        /// <param name="sourcePool">对象池组件</param>
        /// <param name="playController">播放控制组件</param>
        /// <param name="globalControl">全局控制组件</param>
        /// <param name="enableDebug">是否启用调试模式</param>
        public void Initialize(
            Com_AudioResourceManager resourceManager,
            Com_AudioSourcePool sourcePool,
            Com_AudioPlayController playController,
            Com_AudioGlobalControl globalControl,
            bool enableDebug = false)
        {
            this._resourceManager = resourceManager;
            this._sourcePool = sourcePool;
            this._playController = playController;
            this._globalControl = globalControl;
            this._enableDebug = enableDebug;
        }

        /// <summary>
        /// 启用/禁用调试模式
        /// </summary>
        public void SetDebugEnabled(bool enabled)
        {
            this._enableDebug = enabled;
        }

        /// <summary>
        /// 获取调试信息字符串
        /// </summary>
        /// <returns>调试信息</returns>
        public string GetDebugInfo()
        {
            if (!_enableDebug)
            {
                return "调试模式未启用";
            }

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("=== 音频系统调试信息 ===");

            // 当前播放音效列表
            var playingList = _playController?.GetPlayingAudioList() ?? new List<string>();
            sb.AppendLine($"当前播放音效数量: {playingList.Count}");
            if (playingList.Count > 0)
            {
                sb.AppendLine("播放列表:");
                foreach (var item in playingList)
                {
                    sb.AppendLine($"  - {item}");
                }
            }

            // 全局音量
            sb.AppendLine($"全局音量:");
            sb.AppendLine($"  BGM: {(_globalControl?.GetGlobalVolume(AudioType.BGM) ?? 1f):F2}");
            sb.AppendLine($"  SFX_2D: {(_globalControl?.GetGlobalVolume(AudioType.SFX_2D) ?? 1f):F2}");
            sb.AppendLine($"  SFX_3D: {(_globalControl?.GetGlobalVolume(AudioType.SFX_3D) ?? 1f):F2}");

            // 静音状态
            sb.AppendLine($"静音状态:");
            sb.AppendLine($"  BGM: {(_globalControl?.IsMuted(AudioType.BGM) ?? false)}");
            sb.AppendLine($"  SFX_2D: {(_globalControl?.IsMuted(AudioType.SFX_2D) ?? false)}");
            sb.AppendLine($"  SFX_3D: {(_globalControl?.IsMuted(AudioType.SFX_3D) ?? false)}");

            return sb.ToString();
        }

        /// <summary>
        /// 获取当前播放的音效列表（调试用）
        /// </summary>
        /// <returns>音效名称列表</returns>
        public List<string> GetPlayingAudioList()
        {
            return _playController?.GetPlayingAudioList() ?? new List<string>();
        }

        /// <summary>
        /// 打印调试信息到控制台
        /// </summary>
        public void PrintDebugInfo()
        {
            if (_enableDebug)
            {
                Debug.Log(GetDebugInfo());
            }
        }

        /// <summary>
        /// 在屏幕上显示调试信息（需要GUI组件支持）
        /// </summary>
        /// <param name="rect">显示区域</param>
        public void DrawDebugInfo(Rect rect)
        {
            if (!_enableDebug)
            {
                return;
            }

            // 注意：这需要在OnGUI中调用，或者使用UI组件显示
            // 这里提供一个基础实现，实际使用中可以通过UI Text组件显示
        }
    }
}
